/*
 * Copyright 2019 bingoogolapple
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.bingoogolapple.transformerstip.demo.slice;

import cn.bingoogolapple.transformerstip.SimpleTextTip;
import cn.bingoogolapple.transformerstip.TransformersTip;
import cn.bingoogolapple.transformerstip.demo.RecyclerViewTestActivity;
import cn.bingoogolapple.transformerstip.demo.ResourceTable;
import cn.bingoogolapple.transformerstip.demo.TestActivity;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

/**
 * MainAbilitySlice
 *
 * @author 王浩 邮件:bingoogolapple@gmail.com
 * @since 2021-04-25
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int WIDTH_TRANSFORMERS_TIP = 750;

    private static final int HEIGHT_TRANSFORMERS_TIP = 300;

    private static final int WIDTH_SIMPLE_TEXT_TIP = 700;

    private static final int HEIGHT_SIMPLE_TEXT_TIP = 250;

    private static final int WIDTH_ARROW = 50;

    private static final int HEIGHT_ARROW = 30;

    private static final float RADIUS = 20.0f;

    private static final int VIEW_TWO = 2;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#4CAF50"));
        getWindow().setNavigationBarColor(Color.getIntColor("#4CAF50"));
        initView();
    }

    private void initView() {
        findComponentById(ResourceTable.Id_mb_anchor_one).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                demo1();
            }
        });
        findComponentById(ResourceTable.Id_mb_anchor_two).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                demo2();
            }
        });

        findComponentById(ResourceTable.Id_mb_anchor_three).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                demo3();
            }
        });

        findComponentById(ResourceTable.Id_mb_test).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new TestActivity(), new Intent());
            }
        });

        findComponentById(ResourceTable.Id_mb_recyclerview_test).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new RecyclerViewTestActivity(), new Intent());
            }
        });
    }

    private void demo1() {
        Component anchorView = findComponentById(ResourceTable.Id_mb_anchor_one);
        Component component = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_demo1_tip, null, false);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(RADIUS);
        component.setBackground(shapeElement);
        showTransformersTip(anchorView, component, LayoutAlignment.BOTTOM, true, Color.BLACK);
    }

    /**
     * 方式二：在 Java 代码中设置浮窗位置、浮窗背景
     */
    private void demo2() {
        Component anchorView = findComponentById(ResourceTable.Id_mb_anchor_two);
        Component component = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_demo2_tip, null, false);
        showTransformersTip(anchorView, component, LayoutAlignment.TOP, true,
                new Color(Color.getIntColor("#CCCCCCCC")));
    }

    private void showTransformersTip(Component anchorView, Component component,
                                     int layoutAlignment, boolean isSetColor, Color color) {
        TransformersTip transformersTip = new TransformersTip(anchorView, component) {
            @Override
            public boolean isTouchOutside() {
                return false;
            }

            @Override
            protected void initView(Component contentView) {
                // 点击浮窗中自定按钮关闭浮窗
                contentView.findComponentById(ResourceTable.Id_tv_tip_close)
                        .setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(ohos.agp.components.Component component) {
                                dismissTip();
                            }
                        });
            }
        };
        transformersTip.setSize(WIDTH_TRANSFORMERS_TIP, HEIGHT_TRANSFORMERS_TIP);
        transformersTip.setMode(layoutAlignment);
        transformersTip.setHasArrow(true);
        transformersTip.setCornerRadius(RADIUS);
        transformersTip.setAutoClosable(false);
        transformersTip.setTransparent(false);
        if (isSetColor) {
            transformersTip.setBackColor(color);
        }
        transformersTip.setArrowSize(WIDTH_ARROW, HEIGHT_ARROW);
        transformersTip.showDialog();
    }

    /**
     * 方式三：对于仅有文字的浮窗，可以直接使用 SimpleTextTip
     */
    private void demo3() {
        Component component = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_simple_text, null, false);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(RADIUS);
        component.setBackground(shapeElement);
        Text text = (Text) component.findComponentById(ResourceTable.Id_simple_text);
        text.setText("适用于只有文字的浮窗" + System.lineSeparator() + "不写布局文件" + System.lineSeparator() + "在 Java 代码中设置文本内容属性");
        Component anchorView = findComponentById(ResourceTable.Id_mb_anchor_three);
        SimpleTextTip simpleTextTip = new SimpleTextTip(anchorView, component);
        simpleTextTip.setSize(WIDTH_SIMPLE_TEXT_TIP, HEIGHT_SIMPLE_TEXT_TIP);
        simpleTextTip.setMode(LayoutAlignment.TOP);
        simpleTextTip.setHasArrow(true);
        int height = anchorView.getHeight();
        float[] contentPosition = anchorView.getContentPosition();
        simpleTextTip.setOffset((int) contentPosition[0], (int) contentPosition[1] - height / VIEW_TWO - HEIGHT_ARROW);
        simpleTextTip.setAutoClosable(true);
        simpleTextTip.setTransparent(true);
        simpleTextTip.setArrowSize(WIDTH_ARROW, HEIGHT_ARROW);
        simpleTextTip.setCornerRadius(RADIUS);
        simpleTextTip.setBackColor(new Color(Color.getIntColor("#CCCCCCCC")));
        simpleTextTip.showDialog();
    }
}
